package com.mfnote.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import com.mfnote.view.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
 * 记事本列表适配器
 * @author ZHF
 *
 */
public class WriteAdapter extends BaseAdapter {
	
	private Context context;
	private ArrayList<HashMap<String, Object>> data;

	public WriteAdapter(Context context, ArrayList<HashMap<String, Object>> data) {
		super();
		this.context = context;
		this.data = data;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		ViewHolder holder = null;
		
		if(convertView == null) {    
            convertView = LayoutInflater.from(context).inflate(R.layout.write_listview_item, null);
            //获得ViewHolder对象
			holder = new ViewHolder();
			//导入布局并赋值给convertView
			holder.img=(ImageView) convertView.findViewById(R.id.iv_islocked); //列表加锁图片
			holder.title = (TextView) convertView.findViewById(R.id.write_lv_title);
			holder.time = (TextView) convertView.findViewById(R.id.write_lv_time);
			
			//为View设置标签
			convertView.setTag(holder);
		} else {
			//取出holder
			holder = (ViewHolder) convertView.getTag();
		}
		
//		ImageView img = (ImageView) convertView.findViewById(R.id.iv_islock);  //列表加锁图片
//		TextView title = (TextView) convertView.findViewById(R.id.write_lv_title);
//		TextView time = (TextView) convertView.findViewById(R.id.write_lv_time);
		//显示内容
		holder.title.setText(data.get(position).get("title").toString());
		holder.time.setText(data.get(position).get("time").toString());
		if(Integer.parseInt(data.get(position).get("lock").toString()) == 1) {  //加锁
			holder.img.setVisibility(View.VISIBLE);  //显示出来
		} else{
			holder.img.setVisibility(View.GONE);  //隐藏掉
		}
		
		return convertView;
	}
	
	public final class ViewHolder{
		ImageView img;
		TextView title;
		TextView time;
	}

}
